package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.OptionBean
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_option_aty.*
import kotlinx.android.synthetic.main.read_head.view.*
import java.util.ArrayList

class OptionAty : BaseAty(), SpringView.OnFreshListener {
    private var page=1
    private lateinit var head:View
    private var des=""
    private val datas=ArrayList<OptionBean.DataBean>()
    private lateinit var adp:BaseQuickAdapter<OptionBean.DataBean,BaseViewHolder>
    override fun getContentId(): Int = R.layout.activity_option_aty

    override fun initView() {


        head=LayoutInflater.from(this).inflate(R.layout.read_head,null)
        adp=object :BaseQuickAdapter<OptionBean.DataBean,BaseViewHolder>(R.layout.jiepan_list_item,datas){
            override fun convert(helper: BaseViewHolder?, item: OptionBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.itemTime,MyUtils.timeStampToStrYMD(item?.create_time?.toLong()!!))
                helper?.setText(R.id.itemType,"分类:${item?.cat_name}")
                Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager= LinearLayoutManager(this, LinearLayout.VERTICAL,false)
        adp.addHeaderView(head)
        head.btVip.visibility=View.GONE


        getData()
        spView.setListener(this)
        spView.footer=DefaultFooter(this)
        spView.header=DefaultHeader(this)
    }

    private fun getData() {
        spView.onFinishFreshAndLoad()
        OkGo.post<OptionBean>(Urls.BaseUrl + Urls.OPTION)
                .params("page", page)
                .params("page_size", 10)
                .execute(object : JsonCallBack<OptionBean>(this) {
                    override fun onSuccess(response: Response<OptionBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            datas.addAll(bean?.data)
                            adp.notifyDataSetChanged()
                        }
                    }
                })
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","viewpoint_des")
                .execute(object :JsonCallBack<PhoneBean>(this){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            head.tv2.text=bean?.data.viewpoint_des
                        }
                        else{
                            ToastUtils.toast(this@OptionAty,bean?.msg!!)
                        }
                    }
                })
    }

    override fun initListener() {
        btExit.setOnClickListener{
            finish()
        }
        adp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, OptionDetailAty::class.java)
            intent.putExtra("type",datas[position].type)
//            MyUtils.toast("传入id${datas[position].id}")
            intent.putExtra("id",datas[position].id)
            intent.putExtra("cat_name",datas[position].cat_name)
            startActivity(intent)
        }
    }

    /*上下拉*/
    override fun onLoadmore() {
        page++
        getData()
    }

    override fun onRefresh() {
        page=1
        datas.clear()
        getData()
    }
}
